package com.project.service.impl;

import com.project.entity.po.Employee;
import com.project.exception.ServiceException;
import com.project.mapper.EmployeeMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.EmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    EmployeeMapper employeeMapper;

    @Override
    public ResponseResult<List<Employee>> selectAllEmployee() {
        List<Employee> list = employeeMapper.selectAllEmployee();
        if(list.size() == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,list);
    }

    @Override
    public ResponseResult<Integer> updateEmployeeById(Long id) {
        int i = employeeMapper.updateEmployeeById(id);
        if(i == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,i);
    }

    @Override
    public ResponseResult<List<Employee>> selectEmployeeByName(String name, int status) {
        List<Employee> list = employeeMapper.selectEmployeeByName(name, status);
        if(list.size() == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult<Employee> selectEmployeeById(Long id) {
        Employee employee = employeeMapper.selectEmployeeById(id);
        if (employee == null){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,employee);
    }

    @Override
    public ResponseResult<Integer> insertEmployee(Employee employee) {
        int i = employeeMapper.insertEmployee(employee);
        if(i == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,i);
    }

    @Override
    public ResponseResult<Integer> updateEmployee(Employee employee) {
        int id = employeeMapper.updateEmployee(employee);
        if(id == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,id);
    }

    @Override
    public ResponseResult<Integer> updateEmployeeByDutyId(Long dutyId, Long id) {
        int i = employeeMapper.updateEmployeeByDutyId(dutyId, id);
        if(i == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,i);
    }
}
